package cn.autumnorange.app.common.provider.cacheannotation.strategy;

import cn.autumnorange.app.common.provider.cacheannotation.filter.RedissionFilter;
import org.redisson.api.RedissonClient;

public class RedissonContext implements RedissionFilter {
  private RedissionFilter redissionFilter = null;
  private static volatile RedissonContext redissonContext = null;
  private RedissonClient redissonClient;

  private RedissonContext() {}

  public RedissonContext(RedissonClient redissonClient) {
    this.redissonClient = redissonClient;
  }

  public static RedissonContext getRedissonContext(RedissonClient redissonClient) {
    if (redissonContext == null)
      synchronized (RedissonContext.class) {
        if (redissonContext == null) {
          redissonContext = new RedissonContext(redissonClient);
        }
      }
    return redissonContext;
  }

  @Override
  public Object doFilter(String dataType, String dataKey) {
    return redissionFilter.doFilter(dataType, dataKey);
  }

  @Override
  public void setNextFilter(RedissionFilter redissionFilter) {
    if (this.redissionFilter == null) {
      this.redissionFilter = redissionFilter;
    } else {
      this.redissionFilter.setNextFilter(redissionFilter);
    }
  }

  public RedissionFilter getRedissionFilter() {
    return this.redissionFilter;
  }

  public void setRedissionFilter(RedissionFilter redissionFilter) {
    this.redissionFilter = redissionFilter;
  }

  public RedissonClient getRedissonClient() {
    return redissonClient;
  }

  public void setRedissonClient(RedissonClient redissonClient) {
    this.redissonClient = redissonClient;
  }
}
